#include "mainwidget.h"
#include "ui_mainwidget.h"

mainwidget::mainwidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::mainwidget)
{
    ui->setupUi(this);
    Angle=1;
    scalevalues=1;
    leanvalues=1;
    QPixmap* p=new QPixmap("://OIP-C.jfif");
    item=new pixItem(p);//图元对象
    scene=new QGraphicsScene(this);
    scene->setSceneRect(-200,-200,380,380);//以0，0为中心点的而设置的场景
    scene->addItem(item);
    view=new QGraphicsView(this);
    view->setScene(scene);
    view->setMinimumSize(800,600);
    controlframe=new QFrame(this);

    CreateControlFrameFunc();


    QHBoxLayout* hlayout=new QHBoxLayout(this);
    hlayout->addWidget(view);
     hlayout->addWidget(controlframe);




}

mainwidget::~mainwidget()
{
    delete ui;
}

void mainwidget::CreateControlFrameFunc()
{
    QGroupBox *imagerotate=new QGroupBox(this);
    imagerotate->setTitle("图像旋转");
    QSlider* rotateslid=new QSlider(this);
    rotateslid->setOrientation(Qt::Horizontal);//设置方向
    rotateslid->setRange(0,360);
    QHBoxLayout*rotatelayout=new QHBoxLayout(this);
    rotatelayout->addWidget(rotateslid);
    imagerotate->setLayout(rotatelayout);


    QGroupBox *imagezoom=new QGroupBox(this);
    imagezoom->setTitle("图像缩放");
    QSlider* zoomslid=new QSlider(this);
   zoomslid->setOrientation(Qt::Horizontal);//设置方向
    zoomslid->setRange(0,2*scalevalues);
    QHBoxLayout*zoomlayout=new QHBoxLayout(this);
   zoomlayout->addWidget(zoomslid);
    imagezoom->setLayout(zoomlayout);


    QGroupBox *imageinclined=new QGroupBox(this);
    imageinclined->setTitle("图像倾斜");
    QSlider* inclinedslid=new QSlider(this);
   inclinedslid->setOrientation(Qt::Horizontal);//设置方向
    inclinedslid->setRange(0,2*leanvalues);
    QHBoxLayout*inclinedlayout=new QHBoxLayout(this);
    inclinedlayout->addWidget(inclinedslid);
    imageinclined->setLayout(inclinedlayout);

   connect(rotateslid,&QSlider::valueChanged,this,&mainwidget::angleChange);
   connect(zoomslid,&QSlider::valueChanged,this,&mainwidget::scaleChange);
   connect(inclinedslid,&QSlider::valueChanged,this,&mainwidget::leanChange);
    QVBoxLayout *layout=new QVBoxLayout(this);
    layout->addWidget(imagerotate);
    layout->addWidget(imagezoom);
    layout->addWidget(imageinclined);
    controlframe->setLayout(layout);


}

void mainwidget::angleChange(int val)//旋转槽函数
{   view->rotate(val);
    Angle=val;
}

void mainwidget::scaleChange(int val)
{
    qreal qs;
    if(val>=scalevalues)
    {
        qs=pow(1.1,(val-scalevalues));//缩放因子大于1.0放大，小于缩小
    }
    else
    {
        qs=pow(1.0/1.1,(scalevalues-val));
    }
    view->scale(qs,qs);
    scalevalues=val;
}

void mainwidget::leanChange(int val)
{
    view->shear((val-leanvalues),0);//以leanvalues为标准负数反向倾斜
   leanvalues=val;

}

